/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.dom.client.Element;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.RootPanel;

import fr.openparts.OpenParts.client.OPBinding;
import fr.openparts.OpenParts.client.OPCommand;
import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandFactory;
import fr.openparts.OpenParts.client.OPCommandFailureReason;
import fr.openparts.OpenParts.client.OPMessagesDispatcher;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPPartLoadingRequest;
import fr.openparts.OpenParts.client.OPPartsLoader;
import fr.openparts.OpenParts.client.OPPartsLoaderBinding;
import fr.openparts.OpenParts.client.OPShell;
import fr.openparts.OpenParts.test.gwt.dummyPart.client.DummyPart;

/**
 * GWT tests for OPCommandFactory.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 13 December 2010
 */
public class OPCommandFactoryTestGwt extends GWTTestCase {

    private static final String SERVICE = "dummyService";
    private static final String RESOURCE = "dummyResource";
    private static final String ACTION = "dummyAction";
    private static final String ACTION_1 = "dummyAction1";
    private static final String ACTION_2 = "dummyAction2";
    private static final String EVENT = "dummyEvent";
    private static final String SLOTID = "dummySlotId";
    private static final String PARAM_NAME = "dummyParamName";
    private static final String PARAM_VALUE = "dummyParamValue";
    private static final String MANDATORY_PARAM_1 = "dummyParam1";
    private static final String MANDATORY_PARAM_2 = "dummyParam2";
    private static final String MESSAGE = "dummyMessage";

    private static final String VIEW_1_CONTENT = "viewFactorySimple";
    private static final String VIEW_2_CONTENT = "viewFactoryMandatoryParams";

    private static final int TIMER_TIMEOUT = 5500;
    private static final int TEST_TIMEOUT = 6000;

    private OPCommand command;
    private OPCommandCallback callback;
    private OPParams params;
    private OPCommandFailureReason reason;
    private DummyPart dummyPart;

    private OPPartsLoaderBinding binding;

    private int onSuccessCalled = 0;
    private int onFailCalled = 0;
    private OPCommandFailureReason onFailReason;

    private Element slotElement;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called at the start of each test to set up the action.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        OPPartsLoader.bind();
        dummyPart = new DummyPart();
        dummyPart.bind();
        binding = getPartsLoaderBinding(OPShell.opBindingSharedInstance());
        callback = new OPCommandCallback() {
            @Override
            public void onSuccess() {
                ++onSuccessCalled;
            }

            @Override
            public void onFail(final OPCommandFailureReason reason) {
                ++onFailCalled;
                onFailReason = reason;
            }
        };
        params = new OPParams(PARAM_NAME, PARAM_VALUE);
        reason = OPCommandFailureReason.commandFailureReasonFactory(0, null);
        slotElement = OPShell.getSlot(SLOTID).getElement();
    }

    /**
     * Called at the end of each test to clean up the mess.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtTearDown()
     */
    @Override
    protected final void gwtTearDown() throws Exception {
        super.gwtTearDown();
        command = null;
        onSuccessCalled = 0;
        onFailCalled = 0;
        onFailReason = null;
        RootPanel.getBodyElement().removeChild(slotElement);
        emptyListeners(OPShell.opBindingSharedInstance());
        unbindOPPartsLoader(OPShell.opBindingSharedInstance());
    }

    /**
     * Test load part command factory. Should get back everything we put in the constructor, and no more.
     */
    public final void testLoadPartCommandFactory() {
        command = OPCommandFactory.loadPartCommandFactory(SERVICE, callback);
        assertNotNull("load part command shouldn't be null", command);
        assertEquals("command type should be a load part command", OPCommandFactory.LOAD_PART, command.getType());
        assertEquals("command service should be " + SERVICE, SERVICE, command.getService());
        assertNull("command params should be null", command.getParams());
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
        command.onFail(reason);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
    }

    /**
     * Test trying to create a load part command with a null service. Should raise an assertion error.
     */
    public final void testLoadPartCommandFactoryWithNullService() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.loadPartCommandFactory(null, callback);
                fail("OPCommandFactory should raise an assertion error when trying to build a load part command with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a load part command with a null callback. Should NOT raise an error.
     */
    public final void testLoadPartCommandFactoryWithNullCallback() {
        try {
            command = OPCommandFactory.loadPartCommandFactory(SERVICE, null);
        } catch (Throwable e) {
            fail("OPCommandFactory should NOT raise an error when trying to build a load part command with a null callback.");
        }
    }

    /**
     * Test display view command factory. Should get back everything we put in the constructor, and no more.
     */
    public final void testDisplayViewCommandFactory() {
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, params, SLOTID);
        assertNotNull("display view command shouldn't be null", command);
        assertEquals("command type should be a display view command", OPCommandFactory.DISPLAY_VIEW, command.getType());
        assertEquals("command service should be " + SERVICE, SERVICE, command.getService());
        final OPParams commandParams = command.getParams();
        assertNotNull("command params shouldn't be null", commandParams);
        assertEquals("command param resource should be " + RESOURCE, RESOURCE, commandParams.getParamValue(OPCommandFactory.PARAM_RESOURCE));
        assertEquals("command param action should be " + ACTION, ACTION, commandParams.getParamValue(OPCommandFactory.PARAM_ACTION));
        assertEquals("command param slot id should be " + SLOTID, SLOTID, commandParams.getParamValue(OPCommandFactory.PARAM_SLOT_ID));
        assertEquals("command param " + PARAM_NAME + " should be " + PARAM_VALUE, PARAM_VALUE, commandParams.getParamValue(PARAM_NAME));
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
        command.onFail(reason);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test trying to create a display view command with a null service. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithNullService() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(null, RESOURCE, ACTION, params, SLOTID);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a display view command with a null resource. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithNullResource() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, null, ACTION, params, SLOTID);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a null resource.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a display view command with a null action. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithNullAction() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, null, params, SLOTID);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a null action.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a display view command with a null slot id. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithNullSlotId() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, params, null);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a null slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a display view command with an empty slot id. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithEmptySlotId() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, params, "");
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with an empty slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a display view command with a null params. Should NOT raise an error.
     */
    public final void testDisplayViewCommandFactoryWithNullParams() {
        try {
            command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, null, SLOTID);
        } catch (Throwable e) {
            fail("OPCommandFactory should NOT raise an error when trying to build a display view command with a null params.");
        }
    }

    /**
     * Test trying to create a display view command with a PARAM_RESOURCE parameter. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithResourceParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, new OPParams(OPCommandFactory.PARAM_RESOURCE, PARAM_VALUE), SLOTID);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a PARAM_RESOURCE parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test trying to create a display view command with a PARAM_ACTION parameter. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithActionParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, new OPParams(OPCommandFactory.PARAM_ACTION, PARAM_VALUE), SLOTID);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a PARAM_ACTION parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test trying to create a display view command with a PARAM_SLOT_ID parameter. Should raise an assertion error.
     */
    public final void testDisplayViewCommandFactoryWithSlotIDParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, new OPParams(OPCommandFactory.PARAM_SLOT_ID, PARAM_VALUE), SLOTID);
                fail("OPCommandFactory should raise an assertion error when trying to build a display view command with a PARAM_SLOT_ID parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test dispatch event command factory. Should get back everything we put in the constructor, and no more.
     */
    public final void testDispatchEventCommandFactory() {
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, params);
        assertNotNull("dispatch event command shouldn't be null", command);
        assertEquals("command type should be a dispatch event command", OPCommandFactory.DISPATCH_EVENT, command.getType());
        assertNull("command service should be null", command.getService());
        final OPParams commandParams = command.getParams();
        assertNotNull("command params shouldn't be null", commandParams);
        assertEquals("command param event should be " + EVENT, EVENT, commandParams.getParamValue(OPCommandFactory.PARAM_EVENT));
        assertEquals("command param " + PARAM_NAME + " should be " + PARAM_VALUE, PARAM_VALUE, commandParams.getParamValue(PARAM_NAME));
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
        command.onFail(reason);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test trying to create a dispatch event command with a null event. Should raise an assertion error.
     */
    public final void testDispatchEventCommandFactoryWithNullEvent() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchEventCommandFactory(null, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch event command with a null event.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch event command with a null params. Should NOT raise an error.
     */
    public final void testDispatchEventCommandFactoryWithNullParams() {
        try {
            command = OPCommandFactory.dispatchEventCommandFactory(EVENT, null);
        } catch (Throwable e) {
            fail("OPCommandFactory should NOT raise an error when trying to build a dispatch event command with a null params.");
        }
    }

    /**
     * Test trying to create a dispatch event command with a PARAM_EVENT parameter. Should raise an assertion error.
     */
    public final void testDispatchEventCommandFactoryWithEventParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchEventCommandFactory(EVENT, new OPParams(OPCommandFactory.PARAM_EVENT, PARAM_VALUE));
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch event command with a PARAM_EVENT parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test dispatch event to command factory. Should get back everything we put in the constructor, and no more.
     */
    public final void testDispatchEventToCommandFactory() {
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, params);
        assertNotNull("dispatch event to command shouldn't be null", command);
        assertEquals("command type should be a dispatch event to command", OPCommandFactory.DISPATCH_EVENT_TO, command.getType());
        assertEquals("command service should be " + SERVICE, SERVICE, command.getService());
        final OPParams commandParams = command.getParams();
        assertNotNull("command params shouldn't be null", commandParams);
        assertEquals("command param event should be " + EVENT, EVENT, commandParams.getParamValue(OPCommandFactory.PARAM_EVENT));
        assertEquals("command param " + PARAM_NAME + " should be " + PARAM_VALUE, PARAM_VALUE, commandParams.getParamValue(PARAM_NAME));
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
        command.onFail(reason);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test trying to create a dispatch event to command with a null service. Should raise an assertion error.
     */
    public final void testDispatchEventToCommandFactoryWithNullService() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchEventToCommandFactory(null, EVENT, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch event to command with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch event to command with a null event. Should raise an assertion error.
     */
    public final void testDispatchEventToCommandFactoryWithNullEvent() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, null, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch event to command with a null event.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch event to command with a null params. Should NOT raise an error.
     */
    public final void testDispatchEventToCommandFactoryWithNullParams() {
        try {
            command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, null);
        } catch (Throwable e) {
            fail("OPCommandFactory should NOT raise an error when trying to build a dispatch event to command with a null params.");
        }
    }
    
    /**
     * Test trying to create a dispatch event to command with a PARAM_EVENT parameter. Should raise an assertion error.
     */
    public final void testDispatchEventToCommandFactoryWithEventParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, new OPParams(OPCommandFactory.PARAM_EVENT, PARAM_VALUE));
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch event to command with a PARAM_EVENT parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test dispatch message to embedded view command factory. Should get back everything we put in the constructor, and no more.
     */
    public final void testDipatchMessageToEmbeddedViewCommandFactory() {
        command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, params);
        assertNotNull("dispatch message to embedded view command shouldn't be null", command);
        assertEquals("command type should be a dispatch message to embedded view command", OPCommandFactory.DISPATCH_MESSAGE_TO_EMBEDDED_VIEW,
                command.getType());
        assertNull("command service should be null", command.getService());
        final OPParams commandParams = command.getParams();
        assertNotNull("command params shouldn't be null", commandParams);
        assertEquals("command param message should be " + MESSAGE, MESSAGE, commandParams.getParamValue(OPCommandFactory.PARAM_MESSAGE));
        assertEquals("command param slot id should be " + SLOTID, SLOTID, commandParams.getParamValue(OPCommandFactory.PARAM_SLOT_ID));
        assertEquals("command param " + PARAM_NAME + " should be " + PARAM_VALUE, PARAM_VALUE, commandParams.getParamValue(PARAM_NAME));
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
        command.onFail(reason);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test trying to create a dispatch message to embedded view command with a null message. Should raise an assertion error.
     */
    public final void testDispatchMessageToEmbeddedViewCommandFactoryWithNullMessage() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(null, SLOTID, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to embedded view command with a null message.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch message to embedded view command with a null slot id. Should raise an assertion error.
     */
    public final void testDispatchMessageToEmbeddedViewCommandFactoryWithNullSlotId() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, null, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to embedded view command with a null slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch message to embedded view command with an empty slot id. Should raise an assertion error.
     */
    public final void testDispatchMessageToEmbeddedViewCommandFactoryWithEmptySlotId() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, "", params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to embedded view command with an empty slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch message to embedded view command with a null params. Should NOT raise an error.
     */
    public final void testDispatchMessageToEmbeddedViewCommandFactoryWithNullParams() {
        try {
            command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, null);
        } catch (Throwable e) {
            fail("OPCommandFactory should NOT raise an error when trying to build a dispatch message to embedded view command with a null params.");
        }
    }

    /**
     * Test trying to create a dispatch message to embedded view command with a PARAM_MESSAGE parameter. Should raise an assertion error.
     */
    public final void testDispatchMessageToEmbeddedViewCommandFactoryWithMessageParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, new OPParams(OPCommandFactory.PARAM_MESSAGE, PARAM_VALUE));
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to embedded view command with a PARAM_MESSAGE parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test trying to create a dispatch message to embedded view command with a PARAM_SLOT_ID parameter. Should raise an assertion error.
     */
    public final void testDispatchMessageToEmbeddedViewCommandFactoryWithSlotIDParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, new OPParams(OPCommandFactory.PARAM_SLOT_ID, PARAM_VALUE));
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to embedded view command with a PARAM_SLOT_ID parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test dispatch message to container slot command factory. Should get back everything we put in the constructor, and no more.
     */
    public final void testDipatchMessageToContainerSlotCommandFactory() {
        command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, params);
        assertNotNull("dispatch message to container slot command shouldn't be null", command);
        assertEquals("command type should be a dispatch message to container slot command", OPCommandFactory.DISPATCH_MESSAGE_TO_CONTAINER_SLOT,
                command.getType());
        assertNull("command service should be null", command.getService());
        final OPParams commandParams = command.getParams();
        assertNotNull("command params shouldn't be null", commandParams);
        assertEquals("command param message should be " + MESSAGE, MESSAGE, commandParams.getParamValue(OPCommandFactory.PARAM_MESSAGE));
        assertEquals("command param slot id should be " + SLOTID, SLOTID, commandParams.getParamValue(OPCommandFactory.PARAM_SLOT_ID));
        assertEquals("command param " + PARAM_NAME + " should be " + PARAM_VALUE, PARAM_VALUE, commandParams.getParamValue(PARAM_NAME));
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
        command.onFail(reason);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test trying to create a dispatch message to container slot command with a null message. Should raise an assertion error.
     */
    public final void testDispatchMessageToContainerSlotCommandFactoryWithNullMessage() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToContainerSlotFactory(null, SLOTID, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to container slot command with a null message.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch message to container slot command with a null slot id. Should raise an assertion error.
     */
    public final void testDispatchMessageToContainerSlotCommandFactoryWithNullSlotId() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, null, params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to container slot command with a null slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch message to container slot command with an empty slot id. Should raise an assertion error.
     */
    public final void testDispatchMessageToContainerSlotCommandFactoryWithEmptySlotId() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, "", params);
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to container slot command with an empty slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a dispatch message to container slot command with a null params. Should NOT raise an error.
     */
    public final void testDispatchMessageToContainerSlotCommandFactoryWithNullParams() {
        try {
            command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, null);
        } catch (Throwable e) {
            fail("OPCommandFactory should NOT raise an error when trying to build a dispatch message to container slot command with a null params.");
        }
    }
    
    /**
     * Test trying to create a dispatch message to container slot command with a PARAM_MESSAGE parameter. Should raise an assertion error.
     */
    public final void testDispatchMessageToContainerSlotCommandFactoryWithMessageParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, new OPParams(OPCommandFactory.PARAM_MESSAGE, PARAM_VALUE));
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to container slot command with a PARAM_MESSAGE parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test trying to create a dispatch message to container slot command with a PARAM_SLOT_ID parameter. Should raise an assertion error.
     */
    public final void testDispatchMessageToContainerSlotCommandFactoryWithSlotIDParameter() {
        if (assertionsAreEnabled()) {
            try {
                command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, new OPParams(OPCommandFactory.PARAM_SLOT_ID, PARAM_VALUE));
                fail("OPCommandFactory should raise an assertion error when trying to build a dispatch message to container slot command with a PARAM_SLOT_ID parameter.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test getting the pre command of a null command. Should raise an assertion error.
     */
    public final void testPreCommandForNullCommand() {
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.getPreCommand(null);
                fail("OPCommandFactory should raise an assertion error when trying to get the pre command of a null command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test getting the pre command of a command without a type. Should get a null precommand.
     */
    public final void testPreCommandForCommandWithoutKnownType() {
        command = OPCommandFactory.getPreCommand(OPCommand.commandFactory(-1, null, null, null));
        assertNull("precommand for unknown command should be null", command);
    }

    /**
     * Test getting the pre command for a load part command. Should get null.
     */
    public final void testPreCommandForLoadPartCommand() {
        command = OPCommandFactory.loadPartCommandFactory(SERVICE, null);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNull("precommand for load part command should be null", precommand);
    }

    /**
     * Test getting the pre command for a display view command when service is not already loaded. Should return something not null.
     */
    public final void testPreCommandForDisplayViewCommand() {
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, params, SLOTID);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNotNull("precommand for display view command should not be null", precommand);
        assertEquals("pre command type should be loading", OPCommandFactory.LOAD_PART, precommand.getType());
        assertEquals("pre command service should be " + SERVICE, SERVICE, precommand.getService());
    }

    /**
     * Test getting the pre command for a display view command when service is already loaded. Should get null.
     */
    public final void testPreCommandForDisplayViewCommandWithAlreadyLoadedService() {
        binding.executeLoadingRequest(OPPartLoadingRequest.partLoadingRequestFactory(SERVICE, null));
        binding.notifyLoadingCompleted(SERVICE);
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, params, SLOTID);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNull("precommand for display view command when service is already loaded should be null", precommand);
    }

    /**
     * Test getting the pre command for a dispatch event command. Should get null.
     */
    public final void testPreCommandForDispatchEventCommand() {
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, params);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNull("precommand for dispatch event command should be null", precommand);
    }

    /**
     * Test getting the pre command for a dispatch event command with a service name. Should get something not null.
     */
    public final void testPreCommandForDispatchEventCommandWithService() {
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, params);
        command.setService(SERVICE);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNotNull("precommand for dispatch event command with service should be not null", precommand);
        assertEquals("pre command type should be loading", OPCommandFactory.LOAD_PART, precommand.getType());
        assertEquals("pre command service should be " + SERVICE, SERVICE, precommand.getService());
    }

    /**
     * Test getting the pre command for a dispatch event command with a service name when service is already loaded. Should get null.
     */
    public final void testPreCommandForDispatchEventCommandWithServiceWithAlreadyLoadedService() {
        binding.executeLoadingRequest(OPPartLoadingRequest.partLoadingRequestFactory(SERVICE, null));
        binding.notifyLoadingCompleted(SERVICE);
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, params);
        command.setService(SERVICE);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNull("precommand for dispatch event command with service when service is already loaded should be null", precommand);
    }

    /**
     * Test getting the pre command for a dispatch event to command when service is not already loaded. Should return something not null.
     */
    public final void testPreCommandForDispatchEventToCommand() {
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, params);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNotNull("precommand for dispatch event to command should not be null", precommand);
        assertEquals("pre command type should be loading", OPCommandFactory.LOAD_PART, precommand.getType());
        assertEquals("pre command service should be " + SERVICE, SERVICE, precommand.getService());
    }

    /**
     * Test getting the pre command for a dispatch event to command when service is already loaded. Should get null.
     */
    public final void testPreCommandForDispatchEventToCommandWithAlreadyLoadedService() {
        binding.executeLoadingRequest(OPPartLoadingRequest.partLoadingRequestFactory(SERVICE, null));
        binding.notifyLoadingCompleted(SERVICE);
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, params);
        final OPCommand precommand = OPCommandFactory.getPreCommand(command);
        assertNull("precommand for display view command when service is already loaded should be null", precommand);
    }

    /**
     * Test getting pre command for a malformed display view command that miss a service. Should raise an assertion error.
     */
    public final void testPreCommandForMalformedDisplayViewCommand() {
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.getPreCommand(OPCommand.commandFactory(OPCommandFactory.DISPLAY_VIEW, null, null, null));
                fail("OPCommandFactory should raise an assertion error when trying to get the pre command of a malformed display view command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test getting pre command for a malformed dispatch event to command that miss a service. Should raise an assertion error.
     */
    public final void testPreCommandForMalformedDispatchEventToCommand() {
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.getPreCommand(OPCommand.commandFactory(OPCommandFactory.DISPATCH_EVENT_TO, null, null, null));
                fail("OPCommandFactory should raise an assertion error when trying to get the pre command of a malformed dispatch event to command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test executing a null command. Should raise an assertion error.
     */
    public final void testExecutingNullCommand() {
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(null, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a null command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a command without a type. Should get an UNKNOWN_COMMAND failure.
     */
    public final void testExecutingCommandWithoutKnownType() {
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);

        OPCommandFactory.execute(OPCommand.commandFactory(-1, null, null, callback), null);

        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be unknown command", OPCommandFailureReason.TYPE_UNKNOW_COMMAND, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason command type should be " + -1, "-1", onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_COMMAND_TYPE));
    }

    /**
     * Test executing a load part command.
     */
    public final void testExecuteLoadPartCommand() {
        command = OPCommandFactory.loadPartCommandFactory(SERVICE, null);
        OPCommandFactory.execute(command, null);
        binding.notifyLoadingCompleted(SERVICE);

        final Element scriptElement = DOM.getElementById(OPShell.partLoaderIdForService(SERVICE));
        assertNotNull("There should be a script in the page", scriptElement);
    }

    /**
     * Test executing a load part command with callback.
     */
    public final void testExecuteLoadPartCommandWithCallback() {
        command = OPCommandFactory.loadPartCommandFactory(SERVICE, callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        binding.notifyLoadingCompleted(SERVICE);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a load part command that fail.
     */
    public final void testExecuteLoadPartCommandThatFail() {
        command = OPCommandFactory.loadPartCommandFactory(SERVICE, callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        final Timer timer = new Timer() {
            public void run() {
                assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
                assertEquals("onFail should have been called now", 1, onFailCalled);
                assertNotNull("reason shouldn't be null", onFailReason);
                assertEquals("reason should be timeout", OPCommandFailureReason.TYPE_LOAD_TIMEOUT, onFailReason.getType());
                assertNotNull("reason params shouldn't be null", onFailReason.getParams());
                assertEquals("reason service should be " + SERVICE, SERVICE, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SERVICE));
                finishTest();
            }
        };
        timer.schedule(TIMER_TIMEOUT);
        delayTestFinish(TEST_TIMEOUT);
    }

    /**
     * Test executing a load part command without service. Should raise an assertion error.
     */
    public final void testExecuteLoadPartCommandWithoutService() {
        command = OPCommand.commandFactory(OPCommandFactory.LOAD_PART, null, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a load part command with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a display view command.
     */
    public final void testExecuteDisplayViewCommand() {
        RootPanel.getBodyElement().appendChild(slotElement);

        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        OPCommandFactory.execute(command, null);

        final String viewAsHtml = RootPanel.getBodyElement().getInnerHTML();
        assertNotNull("view as html shouldn't be null", viewAsHtml);
        assertTrue("view 1 content is in the page", viewAsHtml.contains(VIEW_1_CONTENT));
    }

    /**
     * Test executing a display view command with callback.
     */
    public final void testExecuteDisplayViewCommandWithCallback() {
        RootPanel.getBodyElement().appendChild(slotElement);

        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.setCallback(callback);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a display view command without action binded.
     */
    public final void testExecuteDisplayViewCommandWithoutActionBinded() {
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION, null, SLOTID);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no such view", OPCommandFailureReason.TYPE_NO_SUCH_VIEW, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason service should be " + SERVICE, SERVICE, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SERVICE));
        assertEquals("reason resource should be " + RESOURCE, RESOURCE, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_RESOURCE));
        assertEquals("reason action should be " + ACTION, ACTION, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_ACTION));
    }

    /**
     * Test executing a display view command without valid slot.
     */
    public final void testExecuteDisplayViewCommandWithoutValidSlot() {
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no such view", OPCommandFailureReason.TYPE_NO_SUCH_SLOT, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason service should be " + SLOTID, SLOTID, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SLOT_ID));
    }

    /**
     * Test executing a display view command with mandatory params.
     */
    public final void testExecuteDisplayViewCommandWithMandatoryParams() {
        RootPanel.getBodyElement().appendChild(slotElement);

        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_2,
                new OPParams(MANDATORY_PARAM_1, "value1", MANDATORY_PARAM_2, "value2"), SLOTID);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);

        final String viewAsHtml = RootPanel.getBodyElement().getInnerHTML();
        assertNotNull("view as html shouldn't be null", viewAsHtml);
        assertTrue("view 2 content is in the page", viewAsHtml.contains(VIEW_2_CONTENT));
    }

    /**
     * Test executing a display view command with missing mandatory param.
     */
    public final void testExecuteDisplayViewCommandWithMissingMandatoryParam() {
        RootPanel.getBodyElement().appendChild(slotElement);

        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_2, null, SLOTID);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be missing mandatory param", OPCommandFailureReason.TYPE_MISSING_MANDATORY_PARAMS, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason missing param should be " + MANDATORY_PARAM_1, MANDATORY_PARAM_1,
                onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_MISSING_PARAM));
    }

    /**
     * Test executing a display view command with another missing mandatory param.
     */
    public final void testExecuteDisplayViewCommandWithAnotherMissingMandatoryParam() {
        RootPanel.getBodyElement().appendChild(slotElement);

        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_2, new OPParams(MANDATORY_PARAM_1, "value1"), SLOTID);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be missing mandatory param", OPCommandFailureReason.TYPE_MISSING_MANDATORY_PARAMS, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason missing param should be " + MANDATORY_PARAM_2, MANDATORY_PARAM_2,
                onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_MISSING_PARAM));
    }

    /**
     * Test executing a display view command without service. Should raise an assertion error.
     */
    public final void testExecuteDispayViewCommandWithoutService() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPLAY_VIEW, null, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a display view command with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a display view command without params. Should raise an assertion error.
     */
    public final void testExecuteDispayViewCommandWithoutParams() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPLAY_VIEW, SERVICE, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a display view command with a null params.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a display view command without resource. Should raise an assertion error.
     */
    public final void testExecuteDispayViewCommandWithoutResource() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPLAY_VIEW, SERVICE, new OPParams(), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a display view command with a null resource.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a display view command without action. Should raise an assertion error.
     */
    public final void testExecuteDispayViewCommandWithoutAction() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPLAY_VIEW, SERVICE, new OPParams(OPCommandFactory.PARAM_RESOURCE, RESOURCE), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a display view command with a null action.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a display view command without slot id. Should raise an assertion error.
     */
    public final void testExecuteDispayViewCommandWithoutSlotId() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPLAY_VIEW, SERVICE, new OPParams(OPCommandFactory.PARAM_RESOURCE, RESOURCE,
                OPCommandFactory.PARAM_ACTION, ACTION), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a display view command with a null slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch event command.
     */
    public final void testExecuteDispatchEventCommand() {
        assertEquals("Listener should have been called 0 times.", 0, dummyPart.eventHandlerCalls);
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, null);
        OPCommandFactory.execute(command, null);
        assertEquals("Listener should have been called 1 times.", 1, dummyPart.eventHandlerCalls);
    }

    /**
     * Test executing a dispatch event command with callback.
     */
    public final void testExecuteDispatchEventCommandWithCallback() {
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, null);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch event command with parameters.
     */
    public final void testExecuteDispatchEventCommandWithParameters() {
        command = OPCommandFactory.dispatchEventCommandFactory(EVENT, new OPParams(PARAM_NAME, PARAM_VALUE));
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch event command without params. Should raise an assertion error.
     */
    public final void testExecuteDispatchEventCommandWithoutParams() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_EVENT, null, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch event command with a null params.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch event command without event. Should raise an assertion error.
     */
    public final void testExecuteDispatchEventCommandWithoutEvent() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_EVENT, null, new OPParams(), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch event command with a null event.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch event to command.
     */
    public final void testExecuteDispatchEventToCommand() {
        assertEquals("Listener should have been called 0 times.", 0, dummyPart.eventHandlerCalls);
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, null);
        OPCommandFactory.execute(command, null);
        assertEquals("Listener should have been called 1 times.", 1, dummyPart.eventHandlerCalls);
    }

    /**
     * Test executing a dispatch event to command with callback.
     */
    public final void testExecuteDispatchEventToCommandWithCallback() {
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, null);
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch event to command with parameters.
     */
    public final void testExecuteDispatchEventToCommandWithParameters() {
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE, EVENT, new OPParams(PARAM_NAME, PARAM_VALUE));
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch event to command on a not valid service. Should fail.
     */
    public final void testExecuteDispatchEventToCommandWithNoSuchService() {
        command = OPCommandFactory.dispatchEventToCommandFactory(SERVICE + "_dummy", EVENT, new OPParams(PARAM_NAME, PARAM_VALUE));
        command.setCallback(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no such service", OPCommandFailureReason.TYPE_NO_SUCH_SERVICE, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason service should be " + SERVICE + "_dummy", SERVICE + "_dummy",
                onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SERVICE));
    }

    /**
     * Test executing a dispatch event to command without service. Should raise an assertion error.
     */
    public final void testExecuteDispatchEventToCommandWithoutService() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_EVENT_TO, null, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch event to command with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch event to command without params. Should raise an assertion error.
     */
    public final void testExecuteDispatchEventToCommandWithoutParams() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_EVENT_TO, SERVICE, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch event to command with a null params.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch event to command without event. Should raise an assertion error.
     */
    public final void testExecuteDispatchEventToCommandWithoutEvent() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_EVENT_TO, SERVICE, new OPParams(), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch event to command with a null event.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch message to embedded view command.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommand() {
        RootPanel.getBodyElement().appendChild(slotElement);
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        OPCommandFactory.execute(command, null);

        command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch message to embedded view command with params.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithParams() {
        RootPanel.getBodyElement().appendChild(slotElement);
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        OPCommandFactory.execute(command, null);

        command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, new OPParams(PARAM_NAME, PARAM_VALUE));
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch message to embedded view command with an invalid slot id.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithInvalidSlotId() {
        command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no such such id on the page", OPCommandFailureReason.TYPE_NO_SUCH_ID_IN_THE_PAGE, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason slot id should be " + SLOTID, SLOTID, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SLOT_ID));
    }

    /**
     * Test executing a dispatch message to embedded view command with an slot id not attached to an OPSlot slot.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithSlotIdNotAttachedToOPSlot() {
        final Element fakeSlot = DOM.createDiv();
        fakeSlot.setId(SLOTID);
        RootPanel.getBodyElement().appendChild(fakeSlot);

        command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be given id is not a slot", OPCommandFailureReason.TYPE_GIVEN_ID_IS_NOT_A_SLOT, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason slot id should be " + SLOTID, SLOTID, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SLOT_ID));

        RootPanel.getBodyElement().removeChild(fakeSlot);
    }

    /**
     * Test executing a dispatch message to embedded view command without any view attached to the proxy.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithoutViewAttachedToTheProxy() {
        RootPanel.getBodyElement().appendChild(slotElement);

        command = OPCommandFactory.dispatchMessageToEmbeddedViewFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no view attached to proxy", OPCommandFailureReason.TYPE_NO_VIEW_ATTACHED_TO_PROXY, onFailReason.getType());
        assertNull("reason params should be null", onFailReason.getParams());
    }

    /**
     * Test executing a dispatch message to embedded view command without params. Should raise an assertion error.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithoutParams() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_MESSAGE_TO_EMBEDDED_VIEW, null, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch message to embedded view command with a null params.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch message to embedded view command without message. Should raise an assertion error.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithoutMessage() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_MESSAGE_TO_EMBEDDED_VIEW, null, new OPParams(), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch message to embedded view command with a null message.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch message to embedded view command without slot id. Should raise an assertion error.
     */
    public final void testExecuteDispatchMessageToEmbeddedViewCommandWithoutSlotId() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_MESSAGE_TO_EMBEDDED_VIEW, null, new OPParams(OPCommandFactory.PARAM_MESSAGE, MESSAGE),
                null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch message to embedded view command with a null slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch message to container slot command.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommand() {
        RootPanel.getBodyElement().appendChild(slotElement);
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        OPCommandFactory.execute(command, null);

        command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch message to container slot command with params.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithParams() {
        RootPanel.getBodyElement().appendChild(slotElement);
        command = OPCommandFactory.displayViewCommandFactory(SERVICE, RESOURCE, ACTION_1, null, SLOTID);
        OPCommandFactory.execute(command, null);

        command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, new OPParams(PARAM_NAME, PARAM_VALUE));
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test executing a dispatch message to container slot command with an invalid slot id.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithInvalidSlotId() {
        command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no such such id on the page", OPCommandFailureReason.TYPE_NO_SUCH_ID_IN_THE_PAGE, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason slot id should be " + SLOTID, SLOTID, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SLOT_ID));
    }

    /**
     * Test executing a dispatch message to container slot command with an slot id not attached to an OPSlot slot.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithSlotIdNotAttachedToOPSlot() {
        final Element fakeSlot = DOM.createDiv();
        fakeSlot.setId(SLOTID);
        RootPanel.getBodyElement().appendChild(fakeSlot);

        command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be given id is not a slot", OPCommandFailureReason.TYPE_GIVEN_ID_IS_NOT_A_SLOT, onFailReason.getType());
        assertNotNull("reason params shouldn't be null", onFailReason.getParams());
        assertEquals("reason slot id should be " + SLOTID, SLOTID, onFailReason.getParams().getParamValue(OPCommandFailureReason.PARAM_SLOT_ID));

        RootPanel.getBodyElement().removeChild(fakeSlot);
    }

    /**
     * Test executing a dispatch message to container slot command without any slot attached to the proxy.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithoutSlotAttachedToTheProxy() {
        RootPanel.getBodyElement().appendChild(slotElement);

        final OPMessagesDispatcher dispatcher = OPMessagesDispatcher.getDispatcherAttachedToId(SLOTID);
        registerDummyProxyInDispatcher(dispatcher);

        command = OPCommandFactory.dispatchMessageToContainerSlotFactory(MESSAGE, SLOTID, null);
        command.setCallback(callback);

        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        OPCommandFactory.execute(command, null);
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        assertNotNull("reason shouldn't be null", onFailReason);
        assertEquals("reason should be no slot attached to proxy", OPCommandFailureReason.TYPE_NO_SLOT_ATTACHED_TO_PROXY, onFailReason.getType());
        assertNull("reason params should be null", onFailReason.getParams());
    }

    /**
     * Test executing a dispatch message to container slot command without params. Should raise an assertion error.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithoutParams() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_MESSAGE_TO_CONTAINER_SLOT, null, null, null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch message to container slot command with a null params.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch message to container slot command without message. Should raise an assertion error.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithoutMessage() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_MESSAGE_TO_CONTAINER_SLOT, null, new OPParams(), null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch message to container slot command with a null message.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test executing a dispatch message to container slot command without slot id. Should raise an assertion error.
     */
    public final void testExecuteDispatchMessageToContainerSlotCommandWithoutSlotId() {
        command = OPCommand.commandFactory(OPCommandFactory.DISPATCH_MESSAGE_TO_CONTAINER_SLOT, null, new OPParams(OPCommandFactory.PARAM_MESSAGE, MESSAGE),
                null);
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.execute(command, null);
                fail("OPCommandFactory should raise an assertion error when trying to execute a dispatch message to container slot command with a null slot id.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test onSuccessLoadingPart with a null command. Should raise an assertion error.
     */
    public final void testOnSuccessLoadPartWithNullCommand() {
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.onSuccessLoadingPart(null, null);
                fail("OPCommandFactory should raise an assertion error when trying to do an onSuccessLoadingPart with a null command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test onFailLoadPart with a null command. Should raise an assertion error.
     */
    public final void testOnFailLoadPartWithNullCommand() {
        if (assertionsAreEnabled()) {
            try {
                OPCommandFactory.onFailLoadingPart(null, null);
                fail("OPCommandFactory should raise an assertion error when trying to do an onFailLoadingPart with a null command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Register a dummy in the dispatcher.
     * 
     * @param dispatcher
     *            the dispatcher on which to register the dummy.
     */
    private final native void registerDummyProxyInDispatcher(final OPMessagesDispatcher dispatcher) /*-{
        dispatcher.messageFromEmbeddedViewHandlerCallback = null;
    }-*/;

    /**
     * Get the parts loader binding.
     * 
     * @param binding
     *            the general binding.
     * @return the parts loader binding.
     */
    private final native OPPartsLoaderBinding getPartsLoaderBinding(OPBinding opbinding) /*-{
        return opbinding.partsLoader;
    }-*/;

    /**
     * Unbind the @link OPPartsLoader.
     */
    private final native void unbindOPPartsLoader(final OPBinding opbinding) /*-{
        opbinding.partsLoader = null;
    }-*/;

    /**
     * Native helper method to clean up event bus listener.
     */
    private final native void emptyListeners(final OPBinding binding) /*-{
        binding.listeners = {};
    }-*/;

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }
}
